//
//  STDSVerifyTool.m
//  STDSVideo
//
//  Created by 2022_cgp on 2021/1/11.
//  Copyright © 2021 stdstb. All rights reserved.
//

#import "MAVerifyTool.h"

@implementation MAVerifyTool

#pragma mark -- Singleton
+ (instancetype)shareTool {
    static MAVerifyTool *manager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [self new];
    });
    return manager;
}

- (void)verifyTransaction:(SKPaymentTransaction *)transaction
                  success:(void (^)(void))successBlock
                  failure:(void (^)(NSError *))failureBlock {
    [[XYStore defaultStore] base64Receipt:^(NSString *base64Data) {
        [[XYStore defaultStore] fetchProduct:transaction.payment.productIdentifier success:^(SKProduct *product) {
//            NSLog(@"========================\n %@, %@ ,%@, %@ \n================", base64Data, MAUserObj(kPayOrderNo), transaction.transactionIdentifier, MAUserObj(kUserUUidKey));
            if (MAUserObj(kUserUUidKey) && MAUserObj(kPayOrderNo) && !MAStringIsEmpty(base64Data)) {
                [ReqManager payVerifyReceiptnWithParams:@{@"receipt" : base64Data, @"pay_order_no" : MAUserObj(kPayOrderNo), @"transaction_id" : transaction.transactionIdentifier} successed:^(id  _Nonnull object) {
                    MABlock(successBlock);
                } failed:^(NSError * _Nonnull error) {
                    MABlock(failureBlock, error);
                }];
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey:@"重启APP刷新会员信息"}];
                MABlock(failureBlock, error);
            }
        } failure:failureBlock];
    } failure:failureBlock];
}
@end
